package com.up.vms.infrastructure.util;

import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;


public class DateUtil {

    public static final String   FORMAT_yyyyMMdd      = "yyyyMMdd";
    public static final String   SIMPLE_DATE_FORMAT   = "yyyyMMddHHmmss";
    public static final String   DEFAULT_DATE_FORMAT  = "yyyy-MM-dd HH:mm:ss";
    public static final String   TIME_FORMAT  = "HH:mm:ss";
    public static final String   FORMAT_HMS           = "yyyyMMddHHmmss";
    public static final String   FORMAT_YMD           = "yyyy-MM-dd";
//    public static final String   FORMAT_YMDHM         = "yyyy-MM-dd HH:mm";


    /**
     * 解析字符串为日期对象
     *
     * @param value
     *            日期字符串
     * @param format
     *            日期格式,缺省为"yyyy-MM-dd HH:mm:ss"
     * @return Date 转化后的日期值
     */
    public static Date format(String value, String format) {
        if (value == null || value.length() == 0)
            return null;
        if (format == null || format.length() == 0)
            format = DEFAULT_DATE_FORMAT;

        if (format.length() > value.length()) {
            format = format.substring(0, value.length());
        }

        Date result = null;
        SimpleDateFormat df = null;
        if (format != null && format.length() > 0) {
            df = new SimpleDateFormat(format);
            try {
                result = df.parse(value);
            } catch (ParseException pe) {
                result = null;
            }
            if (result != null)
                return result;
        }

        return result;
    }

    /**
     * 将日期转化为字符串
     *
     * @param value
     *            日期值
     * @param format
     *            日期格式,缺省为"yyyy-MM-dd HH:mm:ss"
     * @return 转化后的字符串
     */
    public static String format(Date value, String format) {
        if (value == null)
            return "";

        if (format == null || format.length() == 0)
            format = DEFAULT_DATE_FORMAT;
        DateFormat df = new SimpleDateFormat(format);
        String result = df.format(value);
//        if (result.length() != format.length()) {
//            DevLog.debug(result + " is not equals date format " + format);
//        }
        return result;
    }



    /**
     * 获取当前时间
     *
     * @return 转化后的字符串
     */
    public static String now(){
        Date now = new Date();
        SimpleDateFormat sdf = new SimpleDateFormat(DEFAULT_DATE_FORMAT);
        String dtStr = sdf.format(now);
        return dtStr;

    }

    /**
     * 比较两个日期的相差毫秒数,如果开始日期比结束日期早，则返回正数，否则返回负数。
     *
     * @param start
     *            开始日期
     * @param end
     *            结束日期
     * @return
     */
    public static long compareDate(Date start, Date end) {
        long temp = 0;
        Calendar starts = Calendar.getInstance();
        Calendar ends = Calendar.getInstance();
        starts.setTime(start);
        ends.setTime(end);
        temp = ends.getTime().getTime() - starts.getTime().getTime();
        return temp;
    }

    /**
     * 比较两个日期的相差天数,如果开始日期比结束日期早，则返回正数，否则返回负数。 返回end - start的分钟
     *
     * @param start
     * @param end
     * @return
     */
    public static long compareHours(Date start, Date end) {
        long day = compareDate(start, end);
        return day / 1000 / 60 ;
    }

    /*
     * 将时间转换为时间戳
     */
    public static String dateToStamp(String data) throws ParseException{
        if(null == data){
            return null;
        }
        String res;
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat(DEFAULT_DATE_FORMAT);
        Date date = simpleDateFormat.parse(data);
        long ts = date.getTime();
        res = String.valueOf(ts);
        return res;
    }


    /*
     * 将时间戳转换为时间
     */
    public static String stampToDate(Long data){
        if(null == data){
            return null;
        }
        String res;
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat(DEFAULT_DATE_FORMAT);
        long lt = data;
        Date date = new Date(lt);
        res = simpleDateFormat.format(date);
        return res;
    }


    // 上几个月月第一天
    public static String getPreviousMonthFirst(int count){
        String str = "";
        SimpleDateFormat sdf=new SimpleDateFormat(FORMAT_HMS);

        Calendar lastDate = Calendar.getInstance();
        lastDate.set(Calendar.DATE,1);//设为当前月的1号
        lastDate.add(Calendar.MONTH,-count);//减一个月，变为下月的1号
        //lastDate.add(Calendar.DATE,-1);//减去一天，变为当月最后一天

        str=sdf.format(lastDate.getTime());
        return str;
    }

    /**
     * @param days=n n为-,则取n天前,n为+,则取n天后的日期
     * @param days
     * @return
     */
    public static Date getSomeDaysBeforeAfter(int days){
        Calendar cal = Calendar.getInstance();
        cal.add(Calendar.DATE, days);
        return cal.getTime();
    }

    /**
     * days=n n为-,则取n天前,n为+,则取n天后的日期
     * @param days
     * @param format
     * @return
     */
    public static String getSomeDaysStrBeforeAfter(int days,String format){
        Calendar cal = Calendar.getInstance();
        cal.add(Calendar.DATE, days);
        Date date = cal.getTime();
        SimpleDateFormat sdf = new SimpleDateFormat(format);
        return sdf.format(date);
    }

    /**
     * 根据时间的偏移量(小时为单位)，计算出Job执行时间
     * @param duration
     * @param format
     * @return
     */
    public static Date caculateTriggerTime(String date, int duration,String format){
        if (format == null || format.length() == 0)
            format = DEFAULT_DATE_FORMAT;

        Date  startDate = format(date, format);
        Calendar c = Calendar.getInstance();
        c.setTime(startDate);
        c.add(Calendar.HOUR_OF_DAY, duration);
        Date triggerDate = c.getTime();
        return triggerDate;
    }
}
